package com.oocl.chat.operate.impl;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Map;

import com.oocl.chat.operate.WorkOperate;
import com.oocl.chat.pojo.Message;
import com.oocl.chat.util.MaintainMap;
import com.oocl.chat.util.MapOperate;

public class WorkOperateImpl implements WorkOperate {

	public void downline(Message message,ObjectOutputStream oos) {
		MapOperate.removeUser(message.getSender());
		sendAllExcepItself(message,oos);
		System.out.println("用户"+ message.getSender()+"下线！！！");
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void online(Message message,ObjectOutputStream oos) {
		MapOperate.saveUser(message.getSender(), oos);
		String userList = MapOperate.getUserList(message.getSender());
		sendAllExcepItself(message,oos);
		message.setType(4);
		message.setContent(userList);
		message.setReciever(message.getSender());
		sendSpecify(message,oos);
		System.out.println(message.getSender()+"登录成功！");
	}

	public void shake(Message message,ObjectOutputStream oos) {
		sendAllExcepItself(message,oos);
		System.out.println(message.getSender()+" 发出震屏!!!");
	}

	public void chat(Message message,ObjectOutputStream oos) {
		sendMessage(message,oos);
	}
	private void sendMessage(Message message,ObjectOutputStream oos){
		if((!"*".equals(message.getReciever()))&&message.getType()==0)
		{
			sendSpecify(message,oos);
		}else{
			sendAll(message,oos);
		}
	}
	//发送所有人
		private void sendAll(Message message,ObjectOutputStream oos) {
			Map<String, ObjectOutputStream> mapList = MaintainMap.getInstance().getMap();
			for(String s: mapList.keySet()){
				oos = mapList.get(s);
				try {
					oos.writeObject(message);
//					oos.writeObject(null);
					oos.flush();
				} catch (IOException e) {
					e.printStackTrace();
				}
				System.out.println("用户"+message.getSender()+"发送信息到所有client成功");
				System.out.println(mapList.get(s));
			}
		}
		//发送指定的人
		private void sendSpecify(Message message,ObjectOutputStream oos){
			Map<String, ObjectOutputStream> mapList = MaintainMap.getInstance().getMap();
			for(String s: mapList.keySet()){
				if(s.equals(message.getReciever())){
					oos = mapList.get(s);
					message.setReciever("我");
					try {
						oos.writeObject(message);
//						oos.writeObject(null);
						oos.flush();
					} catch (Exception e) {
						e.printStackTrace();
					}
					System.out.println(message.getSender()+" 发送信息给 "+message.getReciever()+"成功");
				}
			}
		}
		//发送给除了自己的所有人
		private void sendAllExcepItself(Message message,ObjectOutputStream oos){
			Map<String, ObjectOutputStream> mapList = MaintainMap.getInstance().getMap();
			for(String s:mapList.keySet()){
				if(!s.equals(message.getSender())){
					oos = mapList.get(s);
					try {
						oos.writeObject(message);
//						oos.writeObject(null);
						oos.flush();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}


}
